package common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.dao.JdbcDAO;
import common.pojo.MemberInfo;

@Scope("prototype")
@Repository
public class MemberInfoDAO  extends JdbcDAO {
	
	public boolean delete(int id){
		String sql ="delete from memberInfo where memberId = "+id;
		delete(sql);
		return true;
	}
	
	public void save(MemberInfo memberInfo){
		String sql = "insert into memberInfo(memberId,realName,sex,mobile,birthday,ldnum,address) values(?,?,?,?,?,?,?)";
		save(sql, new Object[] {memberInfo.getMemberId(),memberInfo.getRealName(),memberInfo.getSex(),
				memberInfo.getMobile(),
				memberInfo.getBirthday(),memberInfo.getLdnum(),
				memberInfo.getAddress()});
	}
	
	
	public void save(int id){
		String sql = "insert into memberInfo(memberId) values(?)";
		save(sql, new Object[] {id});
	}
	
	public MemberInfo findInfoById(int id) {
		class MInfoMapper implements org.springframework.jdbc.core.RowMapper {
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
				MemberInfo memberInfo = new MemberInfo();
				memberInfo.setMemberId(rs.getInt(1));
				memberInfo.setRealName(rs.getString(2));
				memberInfo.setSex(rs.getBoolean(3));
				memberInfo.setMobile(rs.getString(4));
				memberInfo.setBirthday(rs.getString(5));
				memberInfo.setLdnum(rs.getString(6));
				memberInfo.setAddress(rs.getString(7));
				return memberInfo;
			}
		}
		String sql = "select * from memberInfo where memberId ="+ id;
 		return (MemberInfo) getObject(sql, null, new MInfoMapper());
	}
	
	/**
	 * 更新数据库
	 * 
	 * @param 
	 * @return
	 */
	public boolean update(MemberInfo memberInfo) {
		String sql = "update memberInfo set realName=?, sex=?,  mobile=?,  birthday=?, ldnum=?, address=? where memberId =?";
		update(sql, new Object[] {memberInfo.getRealName(),memberInfo.getSex(),
				memberInfo.getMobile(),
				memberInfo.getBirthday(),memberInfo.getLdnum(),
				memberInfo.getAddress(),memberInfo.getMemberId()});
		return true;
	}
}
